﻿#region Copyright (c) 2008 plexBB Team
/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
#endregion

using System;
using System.Data.Linq;
using PlexBB.Controls;
using System.Web.UI;

namespace PlexBB.Data
{
    public class Module
    {
        private int _moduleID;
        private ModuleType _moduleType;
        private int? _parentID;
        private string _controlType;
        private string _moduleMode;
        private string _langName;
        private int _position;
        private bool _enabled = true;
        private bool _display = true;
        private readonly EntitySet<Module> _modules = new EntitySet<Module>();
        private EntityRef<Module> _parentModule;

        public int ModuleID
        {
            get { return _moduleID; }
        }

        public ModuleType ModuleType
        {
            get { return _moduleType; }
            set { _moduleType = value; }
        }

        public int? ParentID
        {
            get { return _parentID; }
            set { _parentID = value; }
        }

        public string ControlType
        {
            get { return _controlType; }
            set { _controlType = value; }
        }

        public string ModuleMode
        {
            get { return _moduleMode; }
            set { _moduleMode = value; }
        }

        public string LangName
        {
            get { return _langName; }
            set { _langName = value; }
        }

        public int Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        public bool Display
        {
            get { return _display; }
            set { _display = value; }
        }

        public EntitySet<Module> Modules
        {
            get { return _modules; }
            set { _modules.Assign(value); }
        }

        public Module ParentModule
        {
            get { return _parentModule.Entity; }
            set { _parentModule.Entity = value; }
        }

        public bool IsCategory
        {
            get { return ControlType == null; }
        }

        public Control CreateControl()
        {
            if (ControlType == null)
            {
                throw new InvalidOperationException();
            }
            Type type = Type.GetType(ControlType);
            return ForumControl.Create(type);
        }

        //public Control CreateControl()
        //{
        //    if (ControlType == null)
        //    {
        //        throw new InvalidOperationException();
        //    }
        //    Type controlType = Type.GetType(ControlType);
        //    if (controlType.IsSubclassOf(typeof(ForumTemplate)))
        //    {
        //        return ForumTemplate.Create(controlType);
        //    }
        //    else
        //    {
        //        return (Control)Activator.CreateInstance(controlType);
        //    }
        //}
    }

    public enum ModuleType
    {
        Acp = 1,
        Mcp = 2,
        Ucp = 3
    }
}
